<template>
  <div class="register-container">
    <div class="register-box">
      <el-form ref="registerFormRef" :model="registerForm" :rules="registerFormRules" class="login_form">
        <el-form-item class="Form-item">
          <div class="number1">欢迎加入codeliu的小家庭!</div>
        </el-form-item>
<!--        昵称-->
        <el-form-item  prop="name">
          <el-input v-model="registerForm.name" prefix-icon="iconfont icon-users" placeholder="请输入昵称"></el-input>
        </el-form-item>
        <!-- 用户名 -->
        <el-form-item  prop="username">
          <el-input v-model="registerForm.username" prefix-icon="iconfont icon-user" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item  prop="password">
          <el-input v-model="registerForm.password" type="password" prefix-icon="iconfont icon-3702mima" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="Form-item">
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 这是登录表单的数据绑定对象
      registerForm: {
        name: '',
        username: '',
        password: '',
        picture: ''
      },
      // 这是表单的验证规则对象
      registerFormRules: {
        name: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    register () {
      this.$refs.registerFormRef.validate(async valid => {
        if (!valid) return this.$message.error({
          message: '表单校验失败！',
          center: true
        })
        const { data: res } = await this.$http.post('register', this.registerForm)
        if (res.status !== 200) return this.$message.error({
          message: '该用户已存在！',
          center: true
        })
        this.$message.success({
          message: '注册成功！',
          center: true
        })
        // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
        //   1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
        // 2. 通过编程式导航跳转到后台主页，路由地址是 /home
        this.$router.push('/login')
      })
    },
  }
}
</script>

<style lang="less" scoped>
.register-container{
  .register-box{
    width: 50%;
    height: 35rem;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: 20%;
    transform: translate(-50%,-50%);
    .login_form{
      padding: 2rem;
      //position: relative;
      .Form-item{
        text-align: center;
        .number1{
          //width: 167px;
          //transform: translate(-50px,0px);
          //position: absolute;
          font-size: 3rem;
          color:orange;
        }
        .login-btn{
          margin-right: 2rem;
        }
      }
    }
  }
  @media (min-width: 992px) and (max-width: 1300px) {
    .register-box{
      width: 90%;
      margin-top: 40%;
    }

  }
  @media (min-width: 768px) and (max-width: 991px) {
    .register-box{
      width: 90%;
      margin-top: 60%;
      font-size: 2rem;
    }

  }
  @media (max-width: 767px) {
    .register-box{
      width: 90%;
      margin-top: 60%;
      .login_form{
        //position: relative;
        .Form-item{
          .number1{
            //width: 167px;
            //transform: translate(-50px,0px);
            //position: absolute;
            font-size: 2.5rem;
            color:orange;
          }
          .login-btn{
          }
        }
      }
    }
  }
}
</style>
